//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SquareRootAndersen.h"
using namespace Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses;
#include <gen/QL/Models/Marketmodels/Evolvers/MarketModelVolProcess.h>
using namespace Cephei::QL::Models::Marketmodels::Evolvers;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::CSquareRootAndersen (Double meanLevel, Double reversionSpeed, Double volVar, Double v0, Cephei::Core::IVector<Double>^ evolutionTimes, UInt64 numberSubSteps_, Double w1, Double w2, Microsoft::FSharp::Core::FSharpOption<Double>^ cutPoint) : CMarketModelVolProcess(CSquareRootAndersen::typeid)
{
    try
    {
#ifdef HANDLE
        _phSquareRootAndersen = NULL;
#endif
        QuantLib::Real _meanLevel = (QuantLib::Real)ValueHelper::Convert (meanLevel); //d
        QuantLib::Real _reversionSpeed = (QuantLib::Real)ValueHelper::Convert (reversionSpeed); //d
        QuantLib::Real _volVar = (QuantLib::Real)ValueHelper::Convert (volVar); //d
        QuantLib::Real _v0 = (QuantLib::Real)ValueHelper::Convert (v0); //d
        evolutionTimes->Lock();
        INativeVector<Double>^ _NCIevolutionTimes = evolutionTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCevolutionTimes = safe_cast<CDoubleVector^>(_NCIevolutionTimes);
        std::vector<QuantLib::Real>& _evolutionTimes = static_cast<std::vector<QuantLib::Real>&> (_NCevolutionTimes->GetReference ());
        QuantLib::Size _numberSubSteps_ = (QuantLib::Size)ValueHelper::Convert (numberSubSteps_); //d
        QuantLib::Real _w1 = (QuantLib::Real)ValueHelper::Convert (w1); //d
        QuantLib::Real _w2 = (QuantLib::Real)ValueHelper::Convert (w2); //d
        QuantLib::Real _cutPoint = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (cutPoint) ? (QuantLib::Real)ValueHelper::Convert (cutPoint->Value) : 1.5); //4
        _ppSquareRootAndersen = new boost::shared_ptr<QuantLib::SquareRootAndersen> (new QuantLib::SquareRootAndersen ( _meanLevel,  _reversionSpeed,  _volVar,  _v0,  _evolutionTimes,  _numberSubSteps_,  _w1,  _w2,  _cutPoint ));
        SetMarketModelVolProcess (boost::dynamic_pointer_cast<QuantLib::MarketModelVolProcess> (*_ppSquareRootAndersen));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (evolutionTimes != nullptr) evolutionTimes->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::CSquareRootAndersen (boost::shared_ptr<QuantLib::SquareRootAndersen>& childNative, Object^ owner) : CMarketModelVolProcess(CSquareRootAndersen::typeid)
{
#ifdef HANDLE
	_phSquareRootAndersen = NULL;
#endif
	_ppSquareRootAndersen = &childNative;
    _ppMarketModelVolProcess = new boost::shared_ptr<QuantLib::MarketModelVolProcess> (boost::dynamic_pointer_cast<QuantLib::MarketModelVolProcess> (*_ppSquareRootAndersen));
}
Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::CSquareRootAndersen (QuantLib::SquareRootAndersen& childNative, Object^ owner) : CMarketModelVolProcess(CSquareRootAndersen::typeid)
{
#ifdef HANDLE
	_phSquareRootAndersen = NULL;
#endif
	_ppSquareRootAndersen = new boost::shared_ptr<QuantLib::SquareRootAndersen> (&childNative);
    _ppMarketModelVolProcess = new boost::shared_ptr<QuantLib::MarketModelVolProcess> (boost::dynamic_pointer_cast<QuantLib::MarketModelVolProcess> (*_ppSquareRootAndersen));
    _SquareRootAndersenOwner = owner;
    _MarketModelVolProcessOwner = owner;
}

Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::CSquareRootAndersen (CSquareRootAndersen^ copy) : CMarketModelVolProcess(CSquareRootAndersen::typeid)
{
#ifdef HANDLE
	_phSquareRootAndersen = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSquareRootAndersen = new boost::shared_ptr<QuantLib::SquareRootAndersen> (copy->GetShared());
        _ppMarketModelVolProcess = new boost::shared_ptr<QuantLib::MarketModelVolProcess> (boost::dynamic_pointer_cast<QuantLib::MarketModelVolProcess> (*_ppSquareRootAndersen));
    }
}
Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::CSquareRootAndersen (PLATFORM::Type^ t) : CMarketModelVolProcess(CSquareRootAndersen::typeid)
{
#ifdef HANDLE
	_phSquareRootAndersen = NULL;
#endif
	if (!t->IsSubclassOf(CSquareRootAndersen::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::CSquareRootAndersen (QuantLib::Handle<QuantLib::SquareRootAndersen>& childNative, Object^ owner)  : CMarketModelVolProcess(CSquareRootAndersen::typeid)
{
	_phSquareRootAndersen = &childNative;
	_ppSquareRootAndersen = &static_cast<boost::shared_ptr<QuantLib::SquareRootAndersen>>(childNative.currentLink());
    _ppMarketModelVolProcess = new boost::shared_ptr<QuantLib::MarketModelVolProcess> (boost::dynamic_pointer_cast<QuantLib::MarketModelVolProcess> (*_ppSquareRootAndersen));
    _SquareRootAndersenOwner = owner;
}
Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::CSquareRootAndersen (QuantLib::Handle<QuantLib::SquareRootAndersen> childNative)  : CMarketModelVolProcess(CSquareRootAndersen::typeid)
{
	_phSquareRootAndersen = &childNative;
	_ppSquareRootAndersen = &static_cast<boost::shared_ptr<QuantLib::SquareRootAndersen>>(childNative.currentLink());
    _ppMarketModelVolProcess = new boost::shared_ptr<QuantLib::MarketModelVolProcess> (boost::dynamic_pointer_cast<QuantLib::MarketModelVolProcess> (*_ppSquareRootAndersen));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::CSquareRootAndersen (QuantLib::SquareRootAndersen childNative)  : CMarketModelVolProcess(CSquareRootAndersen::typeid)
{
#ifdef HANDLE
	_phSquareRootAndersen = NULL;
#endif
	_ppSquareRootAndersen = new boost::shared_ptr<QuantLib::SquareRootAndersen> (new QuantLib::SquareRootAndersen (childNative));
    _ppMarketModelVolProcess = new boost::shared_ptr<QuantLib::MarketModelVolProcess> (boost::dynamic_pointer_cast<QuantLib::MarketModelVolProcess> (*_ppSquareRootAndersen));
}
#endif

Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::~CSquareRootAndersen ()
{
    if (_ppSquareRootAndersen != NULL)
    {
	    delete _ppSquareRootAndersen;
        _ppSquareRootAndersen = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::!CSquareRootAndersen ()
{
    if (_ppSquareRootAndersen != NULL)
    {
	    delete _ppSquareRootAndersen;
    }
}
QuantLib::SquareRootAndersen& Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::GetReference ()
{
    if (_ppSquareRootAndersen == NULL) throw REFNEW NativeNullException ();
	return **_ppSquareRootAndersen;
}
boost::shared_ptr<QuantLib::SquareRootAndersen>& Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::GetShared ()
{
    if (_ppSquareRootAndersen == NULL) throw REFNEW NativeNullException ();
	return *_ppSquareRootAndersen;
}
QuantLib::SquareRootAndersen* Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::GetPointer ()
{
    if (_ppSquareRootAndersen == NULL) throw REFNEW NativeNullException ();
	return &**_ppSquareRootAndersen;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SquareRootAndersen>& Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::GetHandle ()
{
	if (_phSquareRootAndersen == NULL)
	{
		_phSquareRootAndersen = new Handle<QuantLib::SquareRootAndersen> (*_ppSquareRootAndersen);
	}
	return *_phSquareRootAndersen;
}
#endif
bool Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::HasNative () 
{
	return (_ppSquareRootAndersen != NULL);
}

Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::ISquareRootAndersen^ Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::NextPath::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppSquareRootAndersen)->nextPath ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::Nextstep (Cephei::Core::IVector<Double>^ variates)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        variates->Lock();
        INativeVector<Double>^ _NCIvariates = variates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCvariates = safe_cast<CDoubleVector^>(_NCIvariates);
        std::vector<QuantLib::Real>& _variates = static_cast<std::vector<QuantLib::Real>&> (_NCvariates->GetReference ());
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSquareRootAndersen)->nextstep ( _variates );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (variates != nullptr) variates->Unlock(); //NO2  
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::NumberStateVariables::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppSquareRootAndersen)->numberStateVariables ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::NumberSteps::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppSquareRootAndersen)->numberSteps ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::StateVariables::get ()
{
    try
    {
    	std::vector<QuantLib::Real>& _rv = (std::vector<QuantLib::Real>&)(*_ppSquareRootAndersen)->stateVariables ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::StepSd::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSquareRootAndersen)->stepSd ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen::VariatesPerStep::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppSquareRootAndersen)->variatesPerStep ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::ISquareRootAndersen^ Cephei::QL::Models::Marketmodels::Evolvers::Volprocesses::CSquareRootAndersen_Factory::Create (Double meanLevel, Double reversionSpeed, Double volVar, Double v0, Cephei::Core::IVector<Double>^ evolutionTimes, UInt64 numberSubSteps_, Double w1, Double w2, Microsoft::FSharp::Core::FSharpOption<Double>^ cutPoint)
{
    return REFNEW CSquareRootAndersen ( meanLevel,  reversionSpeed,  volVar,  v0,  evolutionTimes,  numberSubSteps_,  w1,  w2,  cutPoint);
}
